package com.woniu.controller;


import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.vo.UserScoreVO;
import com.woniu.pojo.UserScore;
import com.woniu.service.RankUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/rankingList")
@Slf4j
@Api(tags = "排行榜")
public class RankingListController extends BaseController{

    @Autowired
    private RankUserService rankUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 0/2 * * ? ")
    public void updateRanking() {
        List<UserScoreVO> userScore1 = rankUserService.selectAll();
        redisTemplate.delete("Ranking");
        redisTemplate.opsForZSet().add("Ranking",rankUserService.selectAll(),0);
    }

    @Scheduled(cron = "1 0 0 1 * ? ")
    public void updateRankingMonth() {
        List<UserScoreVO> userScore2 = rankUserService.selectMonth();
        redisTemplate.delete("RankingMonth");
        redisTemplate.opsForZSet().add("RankingMonth",rankUserService.selectMonth(),0);
    }

    @PostMapping("/getRank")
    @ApiOperation("排行榜总榜")
    public Result getRank(@RequestBody UserScoreVO userScoreVO){
        Set ranking = redisTemplate.opsForZSet().reverseRangeWithScores("Ranking",0,10);
        System.out.println(ranking);
        return toDataResult(ranking);
    }

    @PostMapping("/getRankMonth")
    @ApiOperation("排行榜月榜")
    public Result getRankMonth(@RequestBody UserScoreVO userScoreVO){
        Set rankingMonth = redisTemplate.opsForZSet().reverseRangeWithScores("RankingMonth",0,10);
        System.out.println(rankingMonth);
        return toDataResult(rankingMonth);
    }
}
